package com.hmjk.health.util;

import java.util.ArrayList;

import com.hmjk.health.tools.ToolsTime;



public class GlycoseData {
	
	public static final int TYPE_GLUC_BEFORE_DAWN      = 1;
	public static final int TYPE_GLUC_EMPTY_STOMACH    = 2;
	public static final int TYPE_GLUC_AFTER_BREAKFAST  = 3;
	public static final int TYPE_GLUC_BEFORE_LUNCH     = 4;
	public static final int TYPE_GLUC_AFTER_LUNCH      = 5;
	public static final int TYPE_GLUC_BEFORE_DINNER    = 6;
	public static final int TYPE_GLUC_AFTER_DINNER     = 7;
	public static final int TYPE_GLUC_AT_BEDTIME       = 8;
	
	public long upTime = 0;
	public String tid;
	public String subcode;
	private ArrayList<GLY_DETAIL> list = new ArrayList<GLY_DETAIL>();

	
	
	
	
	
	public static class GLY_DETAIL{
		public float bgvalue;
		public long recordTime;
		public int bgtype;
		
		public GLY_DETAIL(float value, long time, int type){
			bgvalue = value;
			recordTime = time;
			bgtype = type;
		}
		
		@Override
		public String toString(){
			return ("GLY_DETAIL(recordTime:"+recordTime+",bgvalue:"+bgvalue+",bgtype:"+bgtype+")");
		}
	}
		
	public GlycoseData(String _tid,String _subcode){
		this.tid = _tid;
		this.subcode = _subcode;
	}
	
	public void insertItem(GLY_DETAIL item){
		list.add(0, item);
	}	

	public ArrayList<GLY_DETAIL> getAddrList() {
		// TODO Auto-generated method stub
		return list;
	}
	
	public boolean isEmpty()
	{
		if(list.size() == 0)
			return true;
		else
			return false;
	}
	
	public int getListSize()
	{
		return list.size();
	}
	
	public  GLY_DETAIL getFirstvalue(int bgtype)
	{
		GLY_DETAIL rlt =null;
	     if(list.size()<=0) return rlt;
	     for(int i=0;i<list.size();i++)
	     {
	     	
	     	 if((list.get(i).bgtype==bgtype))
	     	 {
	     	 	  rlt=list.get(i);
	     	 	  break;
	     	 }
	     }
	     return rlt;		
	}

	public  GLY_DETAIL getBgTypetvalue(int bgtype,long time)
	{
		GLY_DETAIL rlt =null;
		 long timemin;
		 timemin =ToolsTime.get0oclock(time);
		 
	     if(list.size()<=0) return rlt;
	     for(int i=0;i<list.size();i++)
	     {
	     	
		     if((list.get(i).bgtype==bgtype))
	     	 {
	     		  if(list.get(i).recordTime>=timemin)
	     		  {	  
	     	 	      rlt=list.get(i);
	     	 	      break;
	     	 	  }
	     	 }	     	 
	     	 
	     }
	     return rlt;		
	}

	public  GLY_DETAIL getLatestBgTypetvalue(int bgtype,long time)
	{
		 GLY_DETAIL rlt =null;
		 long timemin;
		 timemin =ToolsTime.get0oclock(time);
	     if(list.size()<=0) return rlt;
	     for(int i=0;i<list.size();i++)
	     {
	     	
		     if((list.get(i).bgtype==bgtype))
	     	 {
	     		  if(list.get(i).recordTime>=timemin)
	     		  {	  
	     	 	      rlt=list.get(i);
	     	 	      timemin = list.get(i).recordTime;
	     	 	  }
	     	 }
	     }
	     return rlt;		
	}	
	
	
	public GLY_DETAIL getListItem(int pos)
	{
		if(pos < list.size())
			return list.get(pos);
		else
			return null;
	}

	public int getTypeTotalLowerItems(int bgtype, float thresholdvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue<thresholdvalue)&&(list.get(i).bgtype==bgtype))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	

	public int getTypeTotalHigherItems(int bgtype, float thresholdvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>thresholdvalue)&&(list.get(i).bgtype==bgtype))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}		
	

	public int getTotalLowerItems( float thresholdvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue<thresholdvalue))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	

	public int getTotalHigherItems( float thresholdvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>thresholdvalue))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	
	
	public int getRegionTypeItems(int bgtype, float minvalue,float maxvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>=minvalue)&&(list.get(i).bgvalue<=maxvalue)&&(list.get(i).bgtype==bgtype))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	
	

	public int getTotalRegionItems(float minvalue,float maxvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>=minvalue)&&(list.get(i).bgvalue<=maxvalue))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	
	
	public int getTotalRegionItems(double minvalue,double maxvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>=minvalue)&&(list.get(i).bgvalue<=maxvalue))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	
	
	public int getTotalHighItems(int bgtype, float thresholdvalue)
	{
		 int rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if((list.get(i).bgvalue>thresholdvalue)&&(list.get(i).bgvalue==bgtype))
     	 {
     	 	  rlt++;
     	 }
     }
     return rlt;
	}	


	public double getTypeSum (int bgtype)
	{
		 double rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if(list.get(i).bgtype==bgtype)
     	 {
     	 	  rlt+=list.get(i).bgvalue;
     	 }
     }
     return rlt;
	}	


	public double getTypeAverage (int bgtype)
	{
		 double rlt = 0;
		 int y=0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	
     	 if(list.get(i).bgtype==bgtype)
     	 {
     	 	  rlt+=list.get(i).bgvalue;
     	 	  y++;
     	 }
     }
     if(0==y) return 0.0;
     return rlt/y;
	}	
	
	public double getSum ( )
	{
		 double rlt = 0;
     if(list.size()<=0) return rlt;
     for(int i=0;i<list.size();i++)
     {
     	 	  rlt=list.get(i).bgvalue+rlt;
     }
     return rlt;
	}
	
	public double getmin ( )
	{
		 double rlt =9999999.0;
	     if(list.size()<=0) return 0.0;
	     for(int i=0;i<list.size();i++)
	     {
	    	 if(list.get(i).bgvalue<rlt)
	    	 {
	    		 rlt =  list.get(i).bgvalue;
	    	 }	 
	     }
	     return rlt;
	}	
	
	public double getmax ( )
	{
		 double rlt =-9999999.0;
     if(list.size()<=0) return 0.0;
     for(int i=0;i<list.size();i++)
     {
    	 if(list.get(i).bgvalue>rlt)
    	 {
    		 rlt =  list.get(i).bgvalue;
    	 }	 
     }
     return rlt;
	}	
	
	
	
	
}
